package com.bwie.sixth.house.service.impl;

import com.alibaba.fastjson.JSON;
import com.bwie.sixth.house.dao.HomePageDao;
import com.bwie.sixth.house.dao.NewHouseDao;
import com.bwie.sixth.house.dao.OldHouseDao;
import com.bwie.sixth.house.dao.RentHouseDao;
import com.bwie.sixth.house.entity.NewHouseEntity;
import com.bwie.sixth.house.entity.OldHouseEntity;
import com.bwie.sixth.house.entity.RentHouseEntity;
import com.bwie.sixth.house.service.EsPageService;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service("EsPageService")
public class EsPageServiceImpl implements EsPageService {

    @Autowired
    private HomePageDao homePageDao;
    @Autowired
    private RestHighLevelClient client;
    @Autowired
    private NewHouseDao newHouseDao;
    @Autowired
    private OldHouseDao oldHouseDao;
    @Autowired
    private RentHouseDao rentHouseDao;


    @Scheduled(cron = "0/10 * * * * ?")
    public void updateNewEs() throws IOException {
        List<NewHouseEntity> newHouseEntities = newHouseDao.selectList(null);
        BulkRequest bulkRequest = new BulkRequest();
        for (NewHouseEntity entity : newHouseEntities) {
            String s = JSON.toJSONString(entity);
            bulkRequest.add(new IndexRequest("new").id(String.valueOf(entity.getNewId()))
                    .source(s, XContentType.JSON));
        }

        //执行添加
        BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);

        //是否有错误
        if (bulkResponse.hasFailures()) {
            throw new RuntimeException("上架有错误");
        }

    }
    @Scheduled(cron = "0/10 * * * * ?")
    public void updateRentEs() throws IOException {
        List<RentHouseEntity> rentHouseEntities = rentHouseDao.selectList(null);
        BulkRequest bulkRequest = new BulkRequest();
        for (RentHouseEntity entity : rentHouseEntities) {
            String s = JSON.toJSONString(entity);
            bulkRequest.add(new IndexRequest("rent").id(String.valueOf(entity.getRentId()))
                    .source(s, XContentType.JSON));
        }

        //执行添加
        BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);

        //是否有错误
        if (bulkResponse.hasFailures()) {
            throw new RuntimeException("上架有错误");
        }

    }
    @Scheduled(cron = "0/10 * * * * ?")
    public void updateOldEs() throws IOException {
        List<OldHouseEntity> oldHouseEntities = oldHouseDao.selectList(null);
        BulkRequest bulkRequest = new BulkRequest();
        for (OldHouseEntity entity : oldHouseEntities) {
            String s = JSON.toJSONString(entity);
            bulkRequest.add(new IndexRequest("old").id(String.valueOf(entity.getOldId()))
                    .source(s, XContentType.JSON));
        }

        //执行添加
        BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);

        //是否有错误
        if (bulkResponse.hasFailures()) {
            throw new RuntimeException("上架有错误");
        }

    }
}